package com.wb.mas.ui.auth;

import android.os.Bundle;
import android.text.TextUtils;

import com.gyf.immersionbar.ImmersionBar;
import com.wb.mas.BR;
import com.wb.mas.R;
import com.wb.mas.app.AppViewModelFactory;
import com.wb.mas.databinding.ActAuthWorkInfoBinding;
import com.wb.mas.entity.DicInfoEntity;
import com.wb.mas.entity.LocationWrapper;
import com.wb.mas.ui.view.datepicker.CustomDatePicker;
import com.wb.mas.ui.view.datepicker.DateFormatUtils;
import com.wb.mas.widget.OnPopitemClickLinstener;
import com.wb.mas.widget.SelectPopWindow;

import java.util.ArrayList;
import java.util.List;

import androidx.databinding.ObservableField;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import me.goldze.mvvmhabit.base.BaseActivity;

public class AuthWorkInfoAc extends BaseActivity<ActAuthWorkInfoBinding, AuthWorkInfoViewModel> implements OnPopitemClickLinstener {

    private SelectPopWindow selectPopWindow;
    private CustomDatePicker mDatePicker;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.act_auth_work_info;
    }

    @Override
    public int initVariableId() {
        return BR.authWorkInfoViewModel;
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();


        viewModel.uc.selectPupEvent.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                showDictPopUpWindow(viewModel.getCurrentDictType(), viewModel.getCurrentDictList());
            }
        });
        viewModel.uc.showTimeEvent.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {

                String showTime = viewModel.workTime.get();

                mDatePicker.show(TextUtils.isEmpty(showTime) ? "2009-05-01" : showTime);
            }
        });

        viewModel.uc.showLocationEvent.observe(this, new Observer<LocationWrapper>() {
            @Override
            public void onChanged(LocationWrapper wrapper) {
                showLocationPop(wrapper.contentList, wrapper.titleName, wrapper.binded);
            }
        });


    }


    @Override
    protected void initStatusBar() {
        ImmersionBar.with(this)
                .barColor(me.goldze.mvvmhabit.R.color.white)  //同时自定义状态栏和导航栏颜色，不写默认状态栏为透明色，导航栏为黑色
                .statusBarDarkFont(true)   //状态栏字体是深色，不写默认为亮色
                .navigationBarColor(me.goldze.mvvmhabit.R.color.white)
                .init();  //必须调用方可沉浸式
    }

    private void showLocationPop(final List<String> itemStrList, String titleName, final ObservableField<String> binded) {
        selectPopWindow = new SelectPopWindow(this, itemStrList, titleName);

        selectPopWindow.setOnPoPItemClickLinstener(new OnPopitemClickLinstener() {
            @Override
            public void OnItemClick(String title, int position) {
                if (itemStrList == null || itemStrList.size() == 0 || itemStrList.size() <= position)
                    return;
                binded.set(itemStrList.get(position));
                selectPopWindow.dismiss();

            }
        });

        selectPopWindow.show(this, titleName);
    }

    @Override
    public void initData() {
        super.initData();
        initDatePicker();
    }

    private void initDatePicker() {
        long beginTimestamp = DateFormatUtils.str2Long("1980-01-01", false);
        long endTimestamp = System.currentTimeMillis();


//        viewModel.workTime.set("2009-05-01");
        // 通过时间戳初始化日期，毫秒级别
        mDatePicker = new CustomDatePicker(this, new CustomDatePicker.Callback() {
            @Override
            public void onTimeSelected(long timestamp) {
                viewModel.workTime.set(DateFormatUtils.long2StrYm(timestamp));
            }
        }, beginTimestamp, endTimestamp);
        // 不允许点击屏幕或物理返回键关闭
        mDatePicker.setCancelable(false);
        // 不显示时和分
        mDatePicker.setCanShowPreciseTime(false);
        // 不允许循环滚动
        mDatePicker.setScrollLoop(false);
        // 不允许滚动动画
        mDatePicker.setCanShowAnim(false);
    }

    @Override
    public AuthWorkInfoViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(AuthWorkInfoViewModel.class);
    }

    private void showDictPopUpWindow(String currentDictType, List<DicInfoEntity> currentDictList) {
        String titleName = getPopTitleName(currentDictType);
        List<String> itemStrList = new ArrayList<>();
        for (DicInfoEntity entity : currentDictList) {

            itemStrList.add(entity.getDictValue());
        }
        getPopUpWindow(itemStrList, titleName).show(this, itemStrList.get(0));
    }

    private String getPopTitleName(String currentDictType) {
        return getString(R.string.auth_pi_please_select_hint);
    }


    private SelectPopWindow getPopUpWindow(List<String> itemStrList, String titleName) {
//        if (selectPopWindow == null) {
        selectPopWindow = new SelectPopWindow(this, itemStrList, titleName);

        selectPopWindow.setOnPoPItemClickLinstener(this);
//        } else {
//        }

        return selectPopWindow;
    }

    @Override
    public void OnItemClick(String title, int position) {

        viewModel.onItemSelected(title, position);
        selectPopWindow.dismiss();

    }
}
